public class MySingleList implements ILIst{
    // 1、无头单向非循环链表实现
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;


    @Override
    public void addFirst(int data) {
         ListNode node = new ListNode(data);
         node.next = head.next;
         head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(head == null){
            head = node;
            return;
        }
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    @Override
    public void addIndex(int index, int data) {
        int len = size();
        if(index < 0 || index >len){
            System.out.println("index的位置错误");
            return;
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if (index == len){
            addLast(data);
        }
        ListNode cur = head;
        while (index - 1 != 0){
            cur = cur.next;
            index--;
        }
        ListNode node =new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key)
            {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null ) {
            return;
        }
        if(head.val == key){
            head = head.next;
        }
        ListNode cur = findNodeOfKey(key);
        if(cur == null){
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    private ListNode findNodeOfKey(int key){
        ListNode cur = head;
        while (cur.next != null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        ListNode pre = head;
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                pre.next = cur.next;
                cur = cur.next;
                continue;
            }
            pre = cur;
            cur = cur.next;
        }
        if(head.val == key){
            head = head.next;
        }
    }

    @Override
    public int size() {
        int len = 0;
        ListNode cur = head;
        while (cur != null){
            len++;
            cur = cur.next;
        }
        return len;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null){
            System.out.println(cur.val);
            cur = cur.next;
        }
    }

    public void display(ListNode newHead) {
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //反转链表
    public ListNode reverseList() {
        if(head == null){
            return head;
        }
        ListNode cur = head.next;
        ListNode curN = cur.next;
        head.next = null;
        while (cur != null){
            cur.next = head;
            head = cur;
            cur = curN.next;
        }
        return head;
    }

    //链表的中间结点
    public ListNode middleNode(ListNode head) {
        if(head == null){
            return head;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //返回倒数第 k 个节点
    public int kthToLast(int k) {
        if(head == null){
            return -1;
        }
        if(k <= 0){
            return -1;
        }
        ListNode fast = head;
        ListNode slow = head;
        int cout = 0;
        while(cout != k-1){
            fast = fast.next;
            if(fast == null){
                return -1;
            }
            cout++;
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }

    //链表分割
    public ListNode partition( int x) {
        ListNode as = null;
        ListNode ae = null;
        ListNode bs = null;
        ListNode be = null;
        ListNode cur = head;

        while(cur != null){
            if(cur.val < x){
                if(bs == null){
                    bs = be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if(as == null){
                    as = ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(as != null){
            ae.next = null;
        }
        if(bs == null){
            return as;
        }
        be.next = as;
        return bs;

    }

    //链表的回文结构
    public boolean chkPalindrome() {
        // write code here
        if(head == null) return true;
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow.next;
        while(cur != null){
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }
        ListNode A = head;
        while(A != slow){
            if(A.val != slow.val){
                return false;
            }
            if(A.next == slow){
                return true;
            }
            A = A.next;
            slow = slow.next;
        }
        return true;
    }

    //判断链表中是否有环
    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                return true;
            }
        }
        return false;
    }

    //返回链表开始入环的第一个节点
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        if(fast == null || fast.next == null){
            return null;
        }
        slow = head;
        while(slow != fast){
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pl = headA;
        ListNode ps = headB;
        //1.先求2个链表的长度
        int lenA = 0;
        int lenB = 0;
        while(pl != null) {
            lenA++;
            pl = pl.next;
        }
        while(ps != null) {
            lenB++;
            ps = ps.next;
        }
        pl = headA;
        ps = headB;
        //2. 求一下差值
        int len = lenA -lenB;
        if(len < 0) {
            pl = headB;
            ps = headA;
            len = lenB -lenA;
        }
        //3. 让最长的链表 走len步
        while(len != 0) {
            pl = pl.next;
            len--;
        }
        //4.两个引用同时走 直到他们相遇
        while(pl != ps) {
            pl = pl.next;
            ps = ps.next;
        }
        //没相交
        if(pl == null) {
            return null;
        }
        return pl;
    }
}
